//
//  DDroppingView.swift
//  Drop
//
//  Created by hushaohua on 2017/12/21.
//  Copyright © 2017年 init. All rights reserved.
//

import UIKit

class DDroppingView: UIView {
    
    override init(frame: CGRect) {
        self.dropSize = CGSize(width:frame.size.width, height:frame.height - 15);
        self.intervalSize = CGSize(width: frame.size.width, height: 15);
        super.init(frame: frame);
        self.addSubview(self.intervalLabel);
        self.addSubview(self.dropView);
    }
    
    func setColor(_ color:UIColor) -> Void {
        self.dropView.setDripColor(color);
        self.intervalLabel.textColor = color;
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var dropSize:CGSize = .zero;
    lazy var dropView:DDropView = {
        var view:DDropView = DDropView(frame: CGRect(x:0,y:0,width:self.dropSize.width,height:self.dropSize.height));
        view.backgroundColor = UIColor.clear;
        view.clipsToBounds = true;
        return view;
    }();
    
    var intervalSize:CGSize = .zero;
    lazy var intervalLabel:UILabel = {
        var label:UILabel = UILabel(frame: CGRect(x:0,y:0,width:self.intervalSize.width,height:self.intervalSize.height));
        label.adjustsFontSizeToFitWidth = true;
        label.textAlignment = .center;
        label.textColor = UIColor.black;
        return label;
    }();
    
    override var frame: CGRect{
        didSet{
            self.layout();
        }
    }
    
    func layout() -> Void {
        let size:CGSize = self.frame.size;
        var dropFrame = self.dropView.frame;
        var intervalFrame = self.intervalLabel.frame;
        dropFrame.size = self.dropSize;
        intervalFrame.size = self.intervalSize;
        dropFrame.origin.y = 0;
        dropFrame.origin.x = (size.width - dropFrame.width) / 2;
        intervalFrame.origin.y = size.height - self.intervalSize.height;
        intervalFrame.origin.x = (size.width - intervalFrame.width) / 2;
        self.dropView.frame = dropFrame;
        self.intervalLabel.frame = intervalFrame;
    }
    
    override func layoutSubviews() {
        super.layoutSubviews();
    }
}
